<?php

namespace app\index\controller;

use app\index\model\Coupon;
use app\index\model\CtmCun;
use app\index\model\Ctmmoneyflow;
use app\index\model\CtmVip;
use app\index\model\ExpercardCustom;
use app\index\model\Item;
use app\index\model\CtmStaff;
use app\index\model\Custom;
use app\index\model\ItemCustom;
use app\index\model\Miaosha;
use app\index\model\MiaoshaCustom;
use app\index\model\Moneyflow;
use app\index\model\Order;
use app\index\model\Servicenews;
use app\index\model\Store;
use app\index\model\Vipcard;
use Predis\Client;
use think\Db;
use think\Queue;
use app\index\model\Qianggou;
use app\index\model\QianggouCustom;
use app\index\model\UshareCustom;
use app\index\model\Ushare;
use app\util\Utils;

require_once ROOT_PATH . 'application/util/alipay/aop/AopClient.php';

/*
 * 支付回调
 */

class PayController
{
    //支付宝公钥
    const ALIPAYRSA_PUBLIC_KEY = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAp2rlaJi6lSdjF3GLC8D3m+EYS7ppPMEEvNzzvFrnCdeTdtltkCH6SOrGG5BntYqI6bnOwsv0DqlcGfA4YYvWjZ0UWj4KzUDwW3WEWJh/dKvT5a83gAPzd8TazMPxuxDN3JmSb7pVEe3vxHDY9DUU6F/gJxcvjAjU5BxgQ5wBejJxt4MzjaDQcBC/dhqIswSEJ8kbZupiIMC3Ci1pCgpIma3kyO2rjaH5E2D6ffZL3rCJDYse9PuevcMMqkK9GaQSp/1A2wx2E5o/wYLsTD4+6FWNAGSfJKjvZHSRXegfLP5+jymLTeOyVwj+wx/oUwKwgp8iDFA6cikRQ2Zv+L3iZwIDAQAB';

    /*
     * 支付宝回调
     */
    public function alipayNotify()
    {
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = self::ALIPAYRSA_PUBLIC_KEY;
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");

        //支付宝付款回调

        if ($flag) {
            $responseData = $_POST;

            if ($responseData['trade_status'] == 'TRADE_SUCCESS') {
                //付款成功

                $body = $responseData['body']; //商户订单号
                $out_trade_no = $responseData['out_trade_no']; //商户订单号
                $total_amount = $responseData['total_amount']; //订单金额
                $trade_no = $responseData['trade_no']; //支付宝订单号

                //防止重复请求
                $predis = new Client();
                $mode = config('appconfig.app_production');
                if ($mode) {
                    $predis->select(1);
                }
                $key = 'order_' . $out_trade_no;
                $exists = $predis->exists($key);
                if ($exists) {
                    return;
                }
                $predis->set($key, $out_trade_no);

                $type = substr($out_trade_no, 0, 1);

                $time = time();

                if ($type == Order::TYPE_1) {
                    //商品

                } else if ($type == Order::TYPE_2) {
                    //项目

                    // 写入预约记录如果购买的时候选择了预约时间
                    // 修改订单状态
                    // 修改用户金额
                    // 修改项目销售数量
                    // 修改店铺余额
                    // 写入店铺资金流水
                    // 写入项目购买记录如果项目类型为会员特惠区
                    // 回调成功后写入消息

                    //订单信息
                    $orderRow = Order::with('ordPro,custom,store')
                        ->where('ord_num', '=', $out_trade_no)
                        ->field('id, source, store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                        ->order('add_time DESC')
                        ->find();

                    if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                        $order_id = $orderRow['id'];
                        $source = $orderRow['source'];
                        $store_id = $orderRow['store_id'];
                        $ctm_id = $orderRow['ctm_id'];
                        $staff_id = $orderRow['staff_id'];
                        $starttime = $orderRow['starttime'];
                        $ctmstaff_note = $orderRow['ctmstaff_note'];

                        //订单产品信息
                        $pro_id = $orderRow['ord_pro']['pro_id'];
                        $number = $orderRow['ord_pro']['number'];
                        $item_type = $orderRow['ord_pro']['item_type'];

                        //用户信息
                        $custom_phone = $orderRow['custom']['custom_phone'];
                        $custom_name = $orderRow['custom']['custom_name'];
                        $consume_money = $orderRow['custom']['consume_money'];

                        //店铺信息
                        $store_money = $orderRow['store']['money'];


                        Db::startTrans();

                        $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                        //写入预约记录如果购买的时候选择了预约时间
                        $last_yuyue_id = 0;
                        if ($staff_id == 0) {
                            $ctm_staff_insert_result = true;
                        } else {
                            $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;

                            $ctm_staff_insert_result = CtmStaff::insertGetId([
                                'staff_id' => $staff_id,
                                'ctm_id' => $ctm_id,
                                'item_id' => $pro_id,
                                'store_id' => $store_id,
                                'order_id' => $order_id,
                                'number' => 1,
                                'ctm_name' => $ctm_name,
                                'ctm_phone' => $custom_phone,
                                'status' => Order::STATUS_3,
                                'add_time' => $time,
                                'start_time' => $starttime,
                                'ctmstaff_note' => $ctmstaff_note,
                            ]);

                            $last_yuyue_id = $ctm_staff_insert_result;
                        }

                        //修改订单状态
                        $status = Order::STATUS_2;
                        if ($staff_id != 0) {
                            if ($number == 1) {
                                $status = Order::STATUS_3;
                            }
                        }
                        $order_status_result = Order::where('id', '=', $order_id)->update([
                            'ordtype' => Order::ORDTYPE_3,
                            'status' => $status,
                            'pay_time' => $time,
                            'update_time' => $time,
                            'alipay_trade_no' => $trade_no,
                        ]);

                        //修改用户金额
                        $consume_money = bcadd($consume_money, $total_amount, 2);
                        $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                            'consume_money' => $consume_money,
                        ]);

                        //修改项目销售数量
                        $item_update_result = Item::where('id', '=', $pro_id)->setInc('sell_count', $number);

                        //修改店铺余额
                        /*
                        $new_store_money = bcadd($store_money, $total_amount, 2);
                        $store_money_result = Store::where('id', '=', $store_id)->update([
                            'money' => $new_store_money,
                        ]);
                        */
                        $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                        //写入店铺资金流水
                        /*
                        $flow_last_money = $new_store_money;
                        $flow_insert_result = Moneyflow::insert([
                            'type' => Moneyflow::TYPE_1,
                            'pro_type' => Moneyflow::PRO_TYPE_2,
                            'pay_method' => Moneyflow::PAY_METHOD_3,
                            'store_id' => $store_id,
                            'staff_id' => 0,
                            'ctm_id' => $ctm_id,
                            'money' => $total_amount,
                            'last_money' => $flow_last_money,
                            'flow_number' => $flow_number,
                            'add_time' => $time,
                        ]);
                        */
                        $flow_type = Moneyflow::TYPE_1;
                        $flow_pro_type = Moneyflow::PRO_TYPE_2;
                        $flow_pay_method = Moneyflow::PAY_METHOD_3;
                        $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                        //写入项目购买记录如果项目类型为会员特惠区
                        if ($item_type == 3) {
                            $item_custom_insert_result = ItemCustom::insert([
                                'store_id' => $store_id,
                                'item_id' => $pro_id,
                                'custom_id' => $ctm_id,
                                'number' => $number,
                                'price' => $total_amount,
                                'add_time' => $time,
                            ]);
                        } else {
                            $item_custom_insert_result = true;
                        }

                        if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result && $item_custom_insert_result) {
                            Db::commit();

                            //订单消息--已付款
                            $data = [
                                Servicenews::TITLEID_1 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //收入消息
                            $data = [
                                Servicenews::TITLEID_10 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            if ($staff_id != 0) {
                                //预约消息--老板
                                $data = [
                                    Servicenews::TITLEID_2 => [
                                        'order_id' => $order_id,
                                        'staff_type' => 1,
                                        'yuyue_id' => $last_yuyue_id,
                                    ]
                                ];
                                Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                                //预约消息--美容师
                                $data = [
                                    Servicenews::TITLEID_2 => [
                                        'order_id' => $order_id,
                                        'staff_type' => 2,
                                        'yuyue_id' => $last_yuyue_id,
                                    ]
                                ];
                                Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                            }
                        } else {
                            Db::rollback();
                        }
                    }

                } else if ($type == Order::TYPE_3) {
                    //VIP卡

                    // 修改订单状态
                    // 修改用户金额
                    // 写入或修改客户vip卡余额
                    // 写入vip卡资金流水
                    // 修改对应vip卡销售数量和销售金额
                    // 修改店铺余额
                    // 写入店铺资金流水
                    // 回调成功后写入消息

                    //订单信息
                    $orderRow = Order::with('ordPro,custom,store')
                        ->where('ord_num', '=', $out_trade_no)
                        ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                        ->order('add_time DESC')
                        ->find();

                    if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                        $order_id = $orderRow['id'];
                        $source = $orderRow['source'];
                        $store_id = $orderRow['store_id'];
                        $ctm_id = $orderRow['ctm_id'];

                        //订单产品信息
                        $pro_id = $orderRow['ord_pro']['pro_id'];
                        $number = $orderRow['ord_pro']['number'];

                        //用户信息
                        $custom_phone = $orderRow['custom']['custom_phone'];
                        $custom_name = $orderRow['custom']['custom_name'];
                        $charge_money = $orderRow['custom']['charge_money'];
                        $real_money = $orderRow['custom']['real_money'];
                        $consume_money = $orderRow['custom']['consume_money'];

                        //店铺信息
                        $store_money = $orderRow['store']['money'];

                        //vip卡信息
                        $vipcardRow = Vipcard::where('id', '=', $pro_id)
                            ->field('id, vip_name, vip_lines, sell_count, sell_money')
                            ->find();
                        $vipcard_id = $vipcardRow['id'];
                        $vip_name = $vipcardRow['vip_name'];
                        $vip_lines = $vipcardRow['vip_lines'];
                        $sell_count = $vipcardRow['sell_count'];
                        $sell_money = $vipcardRow['sell_money'];


                        Db::startTrans();

                        $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                        //修改订单状态
                        $order_status_result = Order::where('id', '=', $order_id)->update([
                            'ordtype' => Order::ORDTYPE_3,
                            'status' => Order::STATUS_11,
                            'pay_time' => $time,
                            'update_time' => $time,
                            'alipay_trade_no' => $trade_no,
                        ]);

                        //修改用户金额
                        $charge_money = bcadd($charge_money, $vip_lines, 2);
                        $real_money = bcadd($real_money, $vip_lines, 2);
                        $consume_money = bcadd($consume_money, $vip_lines, 2);
                        $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                            'vip_status' => Custom::VIP_STATUS_2,
                            'charge_money' => $charge_money,
                            'real_money' => $real_money,
                            'consume_money' => $consume_money,
                        ]);

                        //写入或修改客户vip卡余额
                        $ctm_vip_row = CtmVip::where('ctm_id', '=', $ctm_id)
                            ->where('vipcard_id', '=', $pro_id)
                            ->field('id, residue')
                            ->find();
                        if (is_null($ctm_vip_row)) {
                            $ctm_vip_result = CtmVip::insert([
                                'ctm_id' => $ctm_id,
                                'vipcard_id' => $pro_id,
                                'residue' => $vip_lines,
                                'add_time' => $time,
                            ]);

                            $ctmflow_last_money = $vip_lines;
                        } else {
                            $residue = bcadd($ctm_vip_row['residue'], $vip_lines, 2);
                            $ctm_vip_result = CtmVip::where('id', '=', $ctm_vip_row['id'])->update([
                                'residue' => $residue,
                            ]);

                            $ctmflow_last_money = $residue;
                        }

                        //写入vip卡资金流水
                        $ctmflow_insert_result = Ctmmoneyflow::insert([
                            'type' => Ctmmoneyflow::TYPE_1,
                            'item_type' => Ctmmoneyflow::ITEM_TYPE_3,
                            'pay_method' => Ctmmoneyflow::PAY_METHOD_3,
                            'store_id' => $store_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $pro_id,
                            'money' => $vip_lines,
                            'last_money' => $ctmflow_last_money,
                            'flow_number' => $flow_number,
                            'vipid' => $pro_id,
                            'add_time' => $time,
                        ]);

                        //修改对应vip卡销售数量和销售金额
                        $sell_count = $sell_count + $number;
                        $sell_money = bcadd($sell_money, $vip_lines, 2);
                        $vipcard_update_result = Vipcard::where('id', '=', $pro_id)->update([
                            'sell_count' => $sell_count,
                            'sell_money' => $sell_money,
                        ]);

                        //修改店铺余额
                        /*
                        $new_store_money = bcadd($store_money, $total_amount, 2);
                        $store_money_result = Store::where('id', '=', $store_id)->update([
                            'money' => $new_store_money,
                        ]);
                        */
                        $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                        //写入店铺资金流水
                        /*
                        $flow_last_money = $new_store_money;
                        $flow_insert_result = Moneyflow::insert([
                            'type' => Moneyflow::TYPE_1,
                            'pro_type' => Moneyflow::PRO_TYPE_3,
                            'pay_method' => Moneyflow::PAY_METHOD_3,
                            'store_id' => $store_id,
                            'staff_id' => 0,
                            'ctm_id' => $ctm_id,
                            'money' => $total_amount,
                            'last_money' => $flow_last_money,
                            'flow_number' => $flow_number,
                            'add_time' => $time,
                        ]);
                        */
                        $flow_type = Moneyflow::TYPE_1;
                        $flow_pro_type = Moneyflow::PRO_TYPE_3;
                        $flow_pay_method = Moneyflow::PAY_METHOD_3;
                        $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                        if ($order_status_result && $custom_money_result && $ctm_vip_result && $ctmflow_insert_result && $vipcard_update_result && $store_money_result && $flow_insert_result) {
                            Db::commit();

                            //订单消息--已付款
                            $data = [
                                Servicenews::TITLEID_1 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //收入消息
                            $data = [
                                Servicenews::TITLEID_10 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        } else {
                            Db::rollback();
                        }
                    }

                } else if ($type == Order::TYPE_4) {
                    //体验卡

                    // 修改订单状态
                    // 修改用户金额
                    // 写入体验卡购买记录
                    // 修改店铺余额
                    // 写入店铺资金流水
                    // 回调成功后写入消息

                    //订单信息
                    $orderRow = Order::with('ordPro,custom,store')
                        ->where('ord_num', '=', $out_trade_no)
                        ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                        ->order('add_time DESC')
                        ->find();

                    if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                        $order_id = $orderRow['id'];
                        $source = $orderRow['source'];
                        $store_id = $orderRow['store_id'];
                        $ctm_id = $orderRow['ctm_id'];

                        //订单产品信息
                        $pro_id = $orderRow['ord_pro']['pro_id'];
                        $number = $orderRow['ord_pro']['number'];

                        //用户信息
                        $custom_phone = $orderRow['custom']['custom_phone'];
                        $custom_name = $orderRow['custom']['custom_name'];
                        $consume_money = $orderRow['custom']['consume_money'];

                        //店铺信息
                        $store_money = $orderRow['store']['money'];


                        Db::startTrans();

                        $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                        //修改订单状态
                        $order_status_result = Order::where('id', '=', $order_id)->update([
                            'ordtype' => Order::ORDTYPE_3,
                            'status' => Order::STATUS_2,
                            'pay_time' => $time,
                            'update_time' => $time,
                            'alipay_trade_no' => $trade_no,
                        ]);

                        //修改用户金额
                        $consume_money = bcadd($consume_money, $total_amount, 2);
                        $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                            'consume_money' => $consume_money,
                        ]);

                        //写入体验卡购买记录
                        $start_time = $time;
                        $end_time = $start_time + 31 * 24 * 3600;
                        $expercard_custom_insert_result = ExpercardCustom::insert([
                            'store_id' => $store_id,
                            'expercard_id' => $pro_id,
                            'custom_id' => $ctm_id,
                            'price' => $total_amount,
                            'start_time' => $start_time,
                            'end_time' => $end_time,
                            'add_time' => $start_time,
                        ]);

                        //修改店铺余额
                        /*
                        $new_store_money = bcadd($store_money, $total_amount, 2);
                        $store_money_result = Store::where('id', '=', $store_id)->update([
                            'money' => $new_store_money,
                        ]);
                        */
                        $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                        //写入店铺资金流水
                        /*
                        $flow_last_money = $new_store_money;
                        $flow_insert_result = Moneyflow::insert([
                            'type' => Moneyflow::TYPE_1,
                            'pro_type' => Moneyflow::PRO_TYPE_4,
                            'pay_method' => Moneyflow::PAY_METHOD_3,
                            'store_id' => $store_id,
                            'staff_id' => 0,
                            'ctm_id' => $ctm_id,
                            'money' => $total_amount,
                            'last_money' => $flow_last_money,
                            'flow_number' => $flow_number,
                            'add_time' => $time,
                        ]);
                        */
                        $flow_type = Moneyflow::TYPE_1;
                        $flow_pro_type = Moneyflow::PRO_TYPE_4;
                        $flow_pay_method = Moneyflow::PAY_METHOD_3;
                        $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                        if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result) {
                            Db::commit();

                            //订单消息--已付款
                            $data = [
                                Servicenews::TITLEID_1 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //收入消息
                            $data = [
                                Servicenews::TITLEID_10 => [
                                    'order_id' => $order_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        } else {
                            Db::rollback();
                        }
                    }

                } else if ($type == Order::TYPE_5) {
                    //限时抢购

                    // 修改订单状态
                    // 修改用户金额
                    // 写入限时抢购购买记录
                    // 修改店铺余额
                    // 写入店铺资金流水
                    // 写入预约记录如果购买的时候选择了预约时间
                    // 回调成功后写入消息

                    //订单信息
                    $orderRow = Order::with('ordPro,custom,store')
                        ->where('ord_num', '=', $out_trade_no)
                        ->field('id, source, store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                        ->order('add_time DESC')
                        ->find();

                    if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                        $order_id = $orderRow['id'];
                        $source = $orderRow['source'];
                        $store_id = $orderRow['store_id'];
                        $ctm_id = $orderRow['ctm_id'];
                        $staff_id = $orderRow['staff_id'];
                        $starttime = $orderRow['starttime'];
                        $ctmstaff_note = $orderRow['ctmstaff_note'];

                        //订单产品信息
                        $pro_id = $orderRow['ord_pro']['pro_id'];
                        $number = $orderRow['ord_pro']['number'];

                        //限时抢购信息
                        $miaoshaRow = Miaosha::where('id', '=', $pro_id)
                            ->field('id, item_id, number, sell_count')
                            ->find();
                        $item_id = $miaoshaRow['item_id'];
                        $miaosha_number = $miaoshaRow['number'];
                        $miaosha_sell_count = $miaoshaRow['sell_count'];

                        //用户信息
                        $custom_phone = $orderRow['custom']['custom_phone'];
                        $custom_name = $orderRow['custom']['custom_name'];
                        $consume_money = $orderRow['custom']['consume_money'];

                        //店铺信息
                        $store_money = $orderRow['store']['money'];

                        //已付款秒杀
                        $pay_number = 0;
                        $orderList = Db::table('da_order')->alias('o')
                            ->join('da_ord_pro op', 'op.ord_id = o.id', 'INNER')
                            ->where('o.status', '>', Order::STATUS_1)
                            ->where('op.type', '=', Order::TYPE_5)
                            ->where('op.pro_id', '=', $pro_id)
                            ->field('op.number')
                            ->order('o.add_time desc')
                            ->select();
                        if (count($orderList) > 0) {
                            foreach ($orderList as $k => $v) {
                                $pay_number = $pay_number + $v['number'];
                            }
                        }

                        Db::startTrans();

                        $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                        //写入预约记录
                        $last_yuyue_id = 0;
                        if ($staff_id == 0) {
                            $ctm_staff_insert_result = true;
                        } else {
                            $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                            $ctm_staff_insert_result = CtmStaff::insertGetId([
                                'staff_id' => $staff_id,
                                'ctm_id' => $ctm_id,
                                'item_id' => $item_id,
                                'store_id' => $store_id,
                                'order_id' => $order_id,
                                'number' => 1,
                                'ctm_name' => $ctm_name,
                                'ctm_phone' => $custom_phone,
                                'status' => Order::STATUS_3,
                                'add_time' => $time,
                                'start_time' => $starttime,
                                'ctmstaff_note' => $ctmstaff_note,
                            ]);

                            $last_yuyue_id = $ctm_staff_insert_result;
                        }
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_3,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'alipay_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = MiaoshaCustom::insert([
                        'store_id' => $store_id,
                        'miaosha_id' => $pro_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_5,
                        'pay_method' => Moneyflow::PAY_METHOD_3,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_5;
                    $flow_pay_method = Moneyflow::PAY_METHOD_3;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    //抢完修改更新时间
                    if (($miaosha_number + $miaosha_sell_count) <= ($pay_number + $number)) {
                        $miaosha_result = Miaosha::where('id', '=', $pro_id)->update([
                            'updated_time' => $time,
                        ]);
                    } else {
                        $miaosha_result = true;
                    }

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result && $miaosha_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }else if ($type == Order::TYPE_6) {
                    //限时抢购

                    // 修改订单状态
                    // 修改用户金额
                    // 写入限时抢购购买记录
                    // 修改店铺余额
                    // 写入店铺资金流水
                    // 写入预约记录如果购买的时候选择了预约时间
                    // 回调成功后写入消息

                    //订单信息
                    $orderRow = Order::with('ordPro,custom,store')
                        ->where('ord_num', '=', $out_trade_no)
                        ->field('id,source, store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                        ->order('add_time DESC')
                        ->find();

                    if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                        $order_id = $orderRow['id'];
                        $store_id = $orderRow['store_id'];
                        $ctm_id = $orderRow['ctm_id'];
                        $staff_id = $orderRow['staff_id'];
                        $starttime = $orderRow['starttime'];
                        $source = $orderRow['source'];
                        $ctmstaff_note = $orderRow['ctmstaff_note'];

                        //订单产品信息
                        $pro_id = $orderRow['ord_pro']['pro_id'];
                        $number = $orderRow['ord_pro']['number'];

                        //限时抢购信息
                        $miaoshaRow = Qianggou::where('id', '=', $pro_id)
                            ->field('id, item_id, number, sell_count')
                            ->find();
                        $item_id = $miaoshaRow['item_id'];
                        $miaosha_number = $miaoshaRow['number'];
                        $miaosha_sell_count = $miaoshaRow['sell_count'];

                        //用户信息
                        $custom_phone = $orderRow['custom']['custom_phone'];
                        $custom_name = $orderRow['custom']['custom_name'];
                        $consume_money = $orderRow['custom']['consume_money'];

                        //店铺信息
                        $store_money = $orderRow['store']['money'];


                        Db::startTrans();

                        $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                        //写入预约记录
                        $last_yuyue_id = 0;
                        if ($staff_id == 0) {
                            $ctm_staff_insert_result = true;
                        } else {
                            $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                            $ctm_staff_insert_result = CtmStaff::insertGetId([
                                'staff_id' => $staff_id,
                                'ctm_id' => $ctm_id,
                                'item_id' => $item_id,
                                'store_id' => $store_id,
                                'order_id' => $order_id,
                                'number' => 1,
                                'ctm_name' => $ctm_name,
                                'ctm_phone' => $custom_phone,
                                'status' => Order::STATUS_3,
                                'add_time' => $time,
                                'start_time' => $starttime,
                                'ctmstaff_note' => $ctmstaff_note,
                            ]);

                            $last_yuyue_id = $ctm_staff_insert_result;
                        }
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_3,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'alipay_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = QianggouCustom::insert([
                        'store_id' => $store_id,
                        'qianggou_id' => $pro_id,
                        'item_id'=>$item_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_8,
                        'pay_method' => Moneyflow::PAY_METHOD_3,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_8;
                    $flow_pay_method = Moneyflow::PAY_METHOD_3;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type,source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type},{$source} ,{$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

                $predis->del($key);

            }


            echo 'success';
        } else {
            echo 'fail';
        }
    }

    /*
     * 微信回调
     */
    public function wxNotify()
    {
        $responseData = (array)simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);

        //微信付款回调

        if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {

            $out_trade_no = $responseData['out_trade_no']; //商户订单号
            $total_fee = $responseData['total_fee']; //订单金额/分
            $trade_no = $responseData['transaction_id']; //微信订单号

            //防止重复请求
            $predis = new Client();
            $mode = config('appconfig.app_production');
            if ($mode) {
                $predis->select(1);
            }
            $key = 'order_' . $out_trade_no;
            $exists = $predis->exists($key);
            if ($exists) {
                return;
            }
            $predis->set($key, $out_trade_no);

            $total_amount = $total_fee / 100;

            $type = substr($out_trade_no, 0, 1);

            $time = time();

            if ($type == Order::TYPE_1) {
                //商品

            } else if ($type == Order::TYPE_2) {
                //项目

                // 写入预约记录如果购买的时候选择了预约时间
                // 修改订单状态
                // 修改用户金额
                // 修改项目销售数量
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入项目购买记录如果项目类型为会员特惠区
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];
                    $ctmstaff_note = $orderRow['ctmstaff_note'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];
                    $item_type = $orderRow['ord_pro']['item_type'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录如果购买的时候选择了预约时间
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;

                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $pro_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                            'ctmstaff_note' => $ctmstaff_note,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_4,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //修改项目销售数量
                    $item_update_result = Item::where('id', '=', $pro_id)->setInc('sell_count', $number);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_2,
                        'pay_method' => Moneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_2;
                    $flow_pay_method = Moneyflow::PAY_METHOD_4;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    //写入项目购买记录如果项目类型为会员特惠区
                    if ($item_type == 3) {
                        $item_custom_insert_result = ItemCustom::insert([
                            'store_id' => $store_id,
                            'item_id' => $pro_id,
                            'custom_id' => $ctm_id,
                            'number' => $number,
                            'price' => $total_amount,
                            'add_time' => $time,
                        ]);
                    } else {
                        $item_custom_insert_result = true;
                    }

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result && $item_custom_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            } else if ($type == Order::TYPE_3) {
                //VIP卡

                // 修改订单状态
                // 修改用户金额
                // 写入或修改客户vip卡余额
                // 写入vip卡资金流水
                // 修改对应vip卡销售数量和销售金额
                // 修改店铺余额
                // 写入店铺资金流水
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $charge_money = $orderRow['custom']['charge_money'];
                    $real_money = $orderRow['custom']['real_money'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];

                    //vip卡信息
                    $vipcardRow = Vipcard::where('id', '=', $pro_id)
                        ->field('id, vip_name, vip_lines, sell_count, sell_money')
                        ->find();
                    $vipcard_id = $vipcardRow['id'];
                    $vip_name = $vipcardRow['vip_name'];
                    $vip_lines = $vipcardRow['vip_lines'];
                    $sell_count = $vipcardRow['sell_count'];
                    $sell_money = $vipcardRow['sell_money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //修改订单状态
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_4,
                        'status' => Order::STATUS_11,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $charge_money = bcadd($charge_money, $vip_lines, 2);
                    $real_money = bcadd($real_money, $vip_lines, 2);
                    $consume_money = bcadd($consume_money, $vip_lines, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'vip_status' => Custom::VIP_STATUS_2,
                        'charge_money' => $charge_money,
                        'real_money' => $real_money,
                        'consume_money' => $consume_money,
                    ]);

                    //写入或修改客户vip卡余额
                    $ctm_vip_row = CtmVip::where('ctm_id', '=', $ctm_id)
                        ->where('vipcard_id', '=', $pro_id)
                        ->field('id, residue')
                        ->find();
                    if (is_null($ctm_vip_row)) {
                        $ctm_vip_result = CtmVip::insert([
                            'ctm_id' => $ctm_id,
                            'vipcard_id' => $pro_id,
                            'residue' => $vip_lines,
                            'add_time' => $time,
                        ]);

                        $ctmflow_last_money = $vip_lines;
                    } else {
                        $residue = bcadd($ctm_vip_row['residue'], $vip_lines, 2);
                        $ctm_vip_result = CtmVip::where('id', '=', $ctm_vip_row['id'])->update([
                            'residue' => $residue,
                        ]);

                        $ctmflow_last_money = $residue;
                    }

                    //写入vip卡资金流水
                    $ctmflow_insert_result = Ctmmoneyflow::insert([
                        'type' => Ctmmoneyflow::TYPE_1,
                        'item_type' => Ctmmoneyflow::ITEM_TYPE_3,
                        'pay_method' => Ctmmoneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'ctm_id' => $ctm_id,
                        'item_id' => $pro_id,
                        'money' => $vip_lines,
                        'last_money' => $ctmflow_last_money,
                        'flow_number' => $flow_number,
                        'vipid' => $pro_id,
                        'add_time' => $time,
                    ]);

                    //修改对应vip卡销售数量和销售金额
                    $sell_count = $sell_count + $number;
                    $sell_money = bcadd($sell_money, $vip_lines, 2);
                    $vipcard_update_result = Vipcard::where('id', '=', $pro_id)->update([
                        'sell_count' => $sell_count,
                        'sell_money' => $sell_money,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_3,
                        'pay_method' => Moneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_3;
                    $flow_pay_method = Moneyflow::PAY_METHOD_4;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    if ($order_status_result && $custom_money_result && $ctm_vip_result && $ctmflow_insert_result && $vipcard_update_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                    } else {
                        Db::rollback();
                    }
                }

            } else if ($type == Order::TYPE_4) {
                //体验卡

                // 修改订单状态
                // 修改用户金额
                // 写入体验卡购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //修改订单状态
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_4,
                        'status' => Order::STATUS_2,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入体验卡购买记录
                    $start_time = $time;
                    $end_time = $start_time + 31 * 24 * 3600;
                    $expercard_custom_insert_result = ExpercardCustom::insert([
                        'store_id' => $store_id,
                        'expercard_id' => $pro_id,
                        'custom_id' => $ctm_id,
                        'price' => $total_amount,
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'add_time' => $start_time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_4,
                        'pay_method' => Moneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_4;
                    $flow_pay_method = Moneyflow::PAY_METHOD_4;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type,source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type},{$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                    } else {
                        Db::rollback();
                    }
                }

            } else if ($type == Order::TYPE_5) {
                //限时抢购

                // 修改订单状态
                // 修改用户金额
                // 写入限时抢购购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入预约记录如果购买的时候选择了预约时间
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source,store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];
                    $source = $orderRow['source'];
                    $ctmstaff_note = $orderRow['ctmstaff_note'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //限时抢购信息
                    $miaoshaRow = Miaosha::where('id', '=', $pro_id)
                        ->field('id, item_id, number, sell_count')
                        ->find();
                    $item_id = $miaoshaRow['item_id'];
                    $miaosha_number = $miaoshaRow['number'];
                    $miaosha_sell_count = $miaoshaRow['sell_count'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];

                    //已付款秒杀
                    $pay_number = 0;
                    $orderList = Db::table('da_order')->alias('o')
                        ->join('da_ord_pro op', 'op.ord_id = o.id', 'INNER')
                        ->where('o.status', '>', Order::STATUS_1)
                        ->where('op.type', '=', Order::TYPE_5)
                        ->where('op.pro_id', '=', $pro_id)
                        ->field('op.number')
                        ->order('o.add_time desc')
                        ->select();
                    if (count($orderList) > 0) {
                        foreach ($orderList as $k => $v) {
                            $pay_number = $pay_number + $v['number'];
                        }
                    }


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $item_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                            'ctmstaff_note' => $ctmstaff_note,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_4,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = MiaoshaCustom::insert([
                        'store_id' => $store_id,
                        'miaosha_id' => $pro_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_5,
                        'pay_method' => Moneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_5;
                    $flow_pay_method = Moneyflow::PAY_METHOD_4;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    //抢完修改更新时间
                    if (($miaosha_number + $miaosha_sell_count) <= ($pay_number + $number)) {
                        $miaosha_result = Miaosha::where('id', '=', $pro_id)->update([
                            'updated_time' => $time,
                        ]);
                    } else {
                        $miaosha_result = true;
                    }

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result && $miaosha_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            }else if ($type == Order::TYPE_6) {
                //限时抢购

                // 修改订单状态
                // 修改用户金额
                // 写入限时抢购购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入预约记录如果购买的时候选择了预约时间
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime,ctmstaff_note, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];
                    $ctmstaff_note = $orderRow['ctmstaff_note'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //限时抢购信息
                    $miaoshaRow = Qianggou::where('id', '=', $pro_id)
                        ->field('id, item_id, number, sell_count')
                        ->find();
                    $item_id = $miaoshaRow['item_id'];
                    $miaosha_number = $miaoshaRow['number'];
                    $miaosha_sell_count = $miaoshaRow['sell_count'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $item_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                            'ctmstaff_note' => $ctmstaff_note,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_4,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = QianggouCustom::insert([
                        'store_id' => $store_id,
                        'qianggou_id' => $pro_id,
                        'item_id'=>$item_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_8,
                        'pay_method' => Moneyflow::PAY_METHOD_4,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_8;
                    $flow_pay_method = Moneyflow::PAY_METHOD_4;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            }

            $predis->del($key);


            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        }
    }

    /*
     * H5微信回调
     */
    public function htmlWxNotify()
    {
        $responseData = (array)simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);

        if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {

            $out_trade_no = $responseData['out_trade_no']; //商户订单号
            $total_fee = $responseData['total_fee']; //订单金额/分
            $trade_no = $responseData['transaction_id']; //微信订单号

            //防止重复请求
            $predis = new Client();
            $mode = config('appconfig.app_production');
            if ($mode) {
                $predis->select(1);
            }
            $key = 'order_' . $out_trade_no;
            $exists = $predis->exists($key);
            if ($exists) {
                return;
            }
            $predis->set($key, $out_trade_no);

            $total_amount = $total_fee / 100;

            $type = substr($out_trade_no, 0, 1);

            $time = time();

            if ($type == Order::TYPE_1) {
                //商品

            } else if ($type == Order::TYPE_2) {
                //项目

                // 写入预约记录如果购买的时候选择了预约时间
                // 修改订单状态
                // 修改用户金额
                // 修改项目销售数量
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入项目购买记录如果项目类型为会员特惠区
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];
                    $item_type = $orderRow['ord_pro']['item_type'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录如果购买的时候选择了预约时间
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;

                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $pro_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_5,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //修改项目销售数量
                    $item_update_result = Item::where('id', '=', $pro_id)->setInc('sell_count', $number);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_2,
                        'pay_method' => Moneyflow::PAY_METHOD_5,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_2;
                    $flow_pay_method = Moneyflow::PAY_METHOD_5;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    //写入项目购买记录如果项目类型为会员特惠区
                    if ($item_type == 3) {
                        $item_custom_insert_result = ItemCustom::insert([
                            'store_id' => $store_id,
                            'item_id' => $pro_id,
                            'custom_id' => $ctm_id,
                            'number' => $number,
                            'price' => $total_amount,
                            'add_time' => $time,
                        ]);
                    } else {
                        $item_custom_insert_result = true;
                    }

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $item_update_result && $store_money_result && $flow_insert_result && $item_custom_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            } else if ($type == Order::TYPE_4) {
                //体验卡

                // 修改订单状态
                // 修改用户金额
                // 写入体验卡购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime, sharer_id, sharer_type, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $sharer_id = $orderRow['sharer_id'];
                    $sharer_type = $orderRow['sharer_type'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //修改订单状态
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_5,
                        'status' => Order::STATUS_2,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入体验卡购买记录
                    $start_time = $time;
                    $end_time = $start_time + 31 * 24 * 3600;
                    $expercard_custom_insert_result = ExpercardCustom::insert([
                        'store_id' => $store_id,
                        'expercard_id' => $pro_id,
                        'custom_id' => $ctm_id,
                        'price' => $total_amount,
                        'start_time' => $start_time,
                        'end_time' => $end_time,
                        'add_time' => $start_time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_4,
                        'pay_method' => Moneyflow::PAY_METHOD_5,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_4;
                    $flow_pay_method = Moneyflow::PAY_METHOD_5;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

//                    if ($sharer_type == 2) {
//                        //写入代金券
//                        //1.首先查一下店铺有没有体验卷
//
//                        $couponRow = Coupon::where('type', '=', Coupon::TYPE_2)
//                            ->where('store_id', '=', $store_id)
//                            ->field('id, money')
//                            ->find();
//                        if (!is_null($couponRow)) {
//                            //写入用户领取表
//
//                            $ctmcun_insert_result = CtmCun::insert([
//                                'ctm_id' => $sharer_id,
//                                'cun_id' => $couponRow['id'],
//                                'employ' => CtmCun::EMPLOY_2,
//                                'add_time' => $time,
//                                'isdel' => CtmCun::ISDEL_2,
//                            ]);
//
//                            //领取数量加一
//                            $coupon_number_result = Coupon::where('id', '=', $couponRow['id'])->setInc('send_number');
//
//                        } else {
//                            //写入店铺体验卷
//
//                            $coupon_insert_result = Coupon::insertGetId([
//                                'type' => Coupon::TYPE_2,
//                                'store_id' => $store_id,
//                                'coupontype_id' => 1,
//                                'money' => 50.00,
//                                'reach' => 300.00,
//                                'mature' => '9999999999',
//                                'number' => 0,
//                                'send_number' => 0,
//                                'issue' => Coupon::ISSUE_1,
//                                'add_time' => $time,
//                            ]);
//
//                            //写入用户领取表
//                            $ctmcun_insert_result = CtmCun::insert([
//                                'ctm_id' => $sharer_id,
//                                'cun_id' => $coupon_insert_result,
//                                'employ' => CtmCun::EMPLOY_2,
//                                'add_time' => $time,
//                                'isdel' => CtmCun::ISDEL_2,
//                            ]);
//
//                            //领取数量加一
//                            $coupon_number_result = Coupon::where('id', '=', $coupon_insert_result)->setInc('send_number');
//                        }
//                    } else if ($sharer_type == 1) {
//                        $ctmcun_insert_result = true;
//                    }

                    if ($order_status_result && $custom_money_result && $expercard_custom_insert_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                    } else {
                        Db::rollback();
                    }
                }

            } else if ($type == Order::TYPE_5) {
                //限时抢购

                // 修改订单状态
                // 修改用户金额
                // 写入限时抢购购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入预约记录如果购买的时候选择了预约时间
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source, store_id, ctm_id, staff_id, starttime, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $source = $orderRow['source'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //限时抢购信息
                    $miaoshaRow = Miaosha::where('id', '=', $pro_id)
                        ->field('id, item_id, number, sell_count')
                        ->find();
                    $item_id = $miaoshaRow['item_id'];
                    $miaosha_number = $miaoshaRow['number'];
                    $miaosha_sell_count = $miaoshaRow['sell_count'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];

                    //已付款秒杀
                    $pay_number = 0;
                    $orderList = Db::table('da_order')->alias('o')
                        ->join('da_ord_pro op', 'op.ord_id = o.id', 'INNER')
                        ->where('o.status', '>', Order::STATUS_1)
                        ->where('op.type', '=', Order::TYPE_5)
                        ->where('op.pro_id', '=', $pro_id)
                        ->field('op.number')
                        ->order('o.add_time desc')
                        ->select();
                    if (count($orderList) > 0) {
                        foreach ($orderList as $k => $v) {
                            $pay_number = $pay_number + $v['number'];
                        }
                    }


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $item_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_5,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = MiaoshaCustom::insert([
                        'store_id' => $store_id,
                        'miaosha_id' => $pro_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_5,
                        'pay_method' => Moneyflow::PAY_METHOD_5,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_5;
                    $flow_pay_method = Moneyflow::PAY_METHOD_5;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type, source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type}, {$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    //抢完修改更新时间
                    if (($miaosha_number + $miaosha_sell_count) <= ($pay_number + $number)) {
                        $miaosha_result = Miaosha::where('id', '=', $pro_id)->update([
                            'updated_time' => $time,
                        ]);
                    } else {
                        $miaosha_result = true;
                    }

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result && $miaosha_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            }else if ($type == Order::TYPE_6) {
                //限时抢购

                // 修改订单状态
                // 修改用户金额
                // 写入限时抢购购买记录
                // 修改店铺余额
                // 写入店铺资金流水
                // 写入预约记录如果购买的时候选择了预约时间
                // 回调成功后写入消息

                //订单信息
                $orderRow = Order::with('ordPro,custom,store')
                    ->where('ord_num', '=', $out_trade_no)
                    ->field('id, source,store_id, ctm_id, staff_id, starttime, status')
                    ->order('add_time DESC')
                    ->find();

                if (!is_null($orderRow) && $orderRow['status'] == Order::STATUS_1) {
                    $order_id = $orderRow['id'];
                    $store_id = $orderRow['store_id'];
                    $ctm_id = $orderRow['ctm_id'];
                    $staff_id = $orderRow['staff_id'];
                    $starttime = $orderRow['starttime'];
                    $source = $orderRow['source'];

                    //订单产品信息
                    $pro_id = $orderRow['ord_pro']['pro_id'];
                    $number = $orderRow['ord_pro']['number'];

                    //限时抢购信息
                    $miaoshaRow = Qianggou::where('id', '=', $pro_id)
                        ->field('id, item_id, number, sell_count')
                        ->find();
                    $item_id = $miaoshaRow['item_id'];
                    $miaosha_number = $miaoshaRow['number'];
                    $miaosha_sell_count = $miaoshaRow['sell_count'];

                    //用户信息
                    $custom_phone = $orderRow['custom']['custom_phone'];
                    $custom_name = $orderRow['custom']['custom_name'];
                    $consume_money = $orderRow['custom']['consume_money'];

                    //店铺信息
                    $store_money = $orderRow['store']['money'];


                    Db::startTrans();

                    $flow_number = $custom_phone . $time . mt_rand(1000, 9999);

                    //写入预约记录
                    $last_yuyue_id = 0;
                    if ($staff_id == 0) {
                        $ctm_staff_insert_result = true;
                    } else {
                        $ctm_name = strlen($custom_name) > 0 ? $custom_name : $custom_phone;
                        $ctm_staff_insert_result = CtmStaff::insertGetId([
                            'staff_id' => $staff_id,
                            'ctm_id' => $ctm_id,
                            'item_id' => $item_id,
                            'store_id' => $store_id,
                            'order_id' => $order_id,
                            'number' => 1,
                            'ctm_name' => $ctm_name,
                            'ctm_phone' => $custom_phone,
                            'status' => Order::STATUS_3,
                            'add_time' => $time,
                            'start_time' => $starttime,
                        ]);

                        $last_yuyue_id = $ctm_staff_insert_result;
                    }

                    //修改订单状态
                    $status = Order::STATUS_2;
                    if ($staff_id != 0) {
                        if ($number == 1) {
                            $status = Order::STATUS_3;
                        }
                    }
                    $order_status_result = Order::where('id', '=', $order_id)->update([
                        'ordtype' => Order::ORDTYPE_5,
                        'status' => $status,
                        'pay_time' => $time,
                        'update_time' => $time,
                        'wx_trade_no' => $trade_no,
                    ]);

                    //修改用户金额
                    $consume_money = bcadd($consume_money, $total_amount, 2);
                    $custom_money_result = Custom::where('id', '=', $ctm_id)->update([
                        'consume_money' => $consume_money,
                    ]);

                    //写入限时抢购购买记录
                    $miaosha_custom_insert_result = QianggouCustom::insert([
                        'store_id' => $store_id,
                        'qianggou_id' => $pro_id,
                        'item_id'=>$item_id,
                        'custom_id' => $ctm_id,
                        'number' => $number,
                        'price' => $total_amount,
                        'add_time' => $time,
                    ]);

                    //修改店铺余额
                    /*
                    $new_store_money = bcadd($store_money, $total_amount, 2);
                    $store_money_result = Store::where('id', '=', $store_id)->update([
                        'money' => $new_store_money,
                    ]);
                    */
                    $store_money_result = Db::execute("UPDATE da_store SET money = money + {$total_amount} WHERE id = {$store_id}");

                    //写入店铺资金流水
                    /*
                    $flow_last_money = $new_store_money;
                    $flow_insert_result = Moneyflow::insert([
                        'type' => Moneyflow::TYPE_1,
                        'pro_type' => Moneyflow::PRO_TYPE_8,
                        'pay_method' => Moneyflow::PAY_METHOD_5,
                        'store_id' => $store_id,
                        'staff_id' => 0,
                        'ctm_id' => $ctm_id,
                        'money' => $total_amount,
                        'last_money' => $flow_last_money,
                        'flow_number' => $flow_number,
                        'add_time' => $time,
                    ]);
                    */
                    $flow_type = Moneyflow::TYPE_1;
                    $flow_pro_type = Moneyflow::PRO_TYPE_8;
                    $flow_pay_method = Moneyflow::PAY_METHOD_5;
                    $flow_insert_result = Db::execute("INSERT INTO da_moneyflow (type,source, pro_type, pay_method, store_id, staff_id, ctm_id, money, last_money, flow_number, add_time) VALUES ({$flow_type},{$source}, {$flow_pro_type}, {$flow_pay_method}, {$store_id}, 0, {$ctm_id}, {$total_amount}, (SELECT money FROM da_store WHERE id = {$store_id}), {$flow_number}, {$time})");

                    if ($ctm_staff_insert_result && $order_status_result && $custom_money_result && $miaosha_custom_insert_result && $store_money_result && $flow_insert_result) {
                        Db::commit();

                        //订单消息--已付款
                        $data = [
                            Servicenews::TITLEID_1 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        //收入消息
                        $data = [
                            Servicenews::TITLEID_10 => [
                                'order_id' => $order_id,
                            ]
                        ];
                        Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                        if ($staff_id != 0) {
                            //预约消息--老板
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 1,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);

                            //预约消息--美容师
                            $data = [
                                Servicenews::TITLEID_2 => [
                                    'order_id' => $order_id,
                                    'staff_type' => 2,
                                    'yuyue_id' => $last_yuyue_id,
                                ]
                            ];
                            Queue::push('app\job\Queue@sendNotifyMessageFunc', $data, null);
                        }

                    } else {
                        Db::rollback();
                    }
                }

            }

            $predis->del($key);


            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        }


    }
    
    /*
     * 微信回调（优享）
     */
    public function usharewxnotify()
    {
        $responseData = (array)simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);

        //微信付款回调

        if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {
            $out_trade_no = $responseData['out_trade_no']; //商户订单号
            $total_fee = $responseData['total_fee']; //订单金额/分
            $trade_no = $responseData['transaction_id']; //微信订单号
            $total_amount = $total_fee / 100;
            $time = time();
            
            $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->field('id,ushare_id,store_id,ctm_id')->find();
            $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
            ->field('custom_phone')
            ->find();
            
            $data['pay_type']=  Order::ORDTYPE_4;
            $data['pay_time']=  $time;
            $data['wx_trade_no']=  $trade_no;
            $data['status']=  2;
            $data['start_time']=$time;
            $data['end_time']=$time+$ushare['duration']*3600;
            $saveusharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->update($data);
            //助力成功发送消息
            $store_id=$usharecustom['store_id'];
            $order_id=$usharecustom['id'];
            $content = "您成功发起了【[".$itemdata['item_name']."]】优享助力活动，快去分享给更多好友，更快助力成功~";
            $msgContent = Utils::getColoredString($content);
            $pushContent = Utils::getPushString($content);
            $alias = $customdata['custom_phone'] . $store_id;
            $userid = $usharecustom['ctm_id'];
            Utils::sendServicenews(Servicenews::TITLEID_16, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                'title' => '活动消息',
                'content' => $pushContent,
                'alias_type' => Servicenews::ALIAS_TYPE_2,
                'alias' => $alias,
            ]);
            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        }
    }
    
    /*
     * 支付宝回调（优享）
     */
    public function usharealipaynotify()
    {
        $aop = new \AopClient();
        $aop->alipayrsaPublicKey = self::ALIPAYRSA_PUBLIC_KEY;
        $flag = $aop->rsaCheckV1($_POST, NULL, "RSA2");

        //支付宝付款回调

        if ($flag) {
            $responseData = $_POST;

            if ($responseData['trade_status'] == 'TRADE_SUCCESS') {
                //付款成功
                $body = $responseData['body']; //商户订单号
                $out_trade_no = $responseData['out_trade_no']; //商户订单号
                $total_amount = $responseData['total_amount']; //订单金额
                $trade_no = $responseData['trade_no']; //支付宝订单号
                $time = time();
                
                $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->field('id,ushare_id,store_id,ctm_id')->find();
                $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->find();
                $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
                $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
                ->field('custom_phone')
                ->find();
                

                $data['pay_type']=  Order::ORDTYPE_3;
                $data['pay_time']=  $time;
                $data['alipay_trade_no']=  $trade_no;
                $data['status']=  2;
                $data['start_time']=$time;
                $data['end_time']=$time+$ushare['duration']*3600;
                $saveusharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->update($data);
                //助力成功发送消息
                $store_id=$usharecustom['store_id'];
                $order_id=$usharecustom['id'];
                $content = "您成功发起了【[".$itemdata['item_name']."]】优享助力活动，快去分享给更多好友，更快助力成功~";
                $msgContent = Utils::getColoredString($content);
                $pushContent = Utils::getPushString($content);
                $alias = $customdata['custom_phone'] . $store_id;
                $userid = $usharecustom['ctm_id'];
                Utils::sendServicenews(Servicenews::TITLEID_16, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                    'title' => '活动消息',
                    'content' => $pushContent,
                    'alias_type' => Servicenews::ALIAS_TYPE_2,
                    'alias' => $alias,
                ]);
            }
            echo 'success';
        } else {
            echo 'fail';
        }
    }
    
    /*
     * H5微信回调（优享）
     */
    public function htmlusharewxnotify()
    {
        $responseData = (array)simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);

        if ($responseData['return_code'] == 'SUCCESS' && $responseData['result_code'] == 'SUCCESS') {

            $out_trade_no = $responseData['out_trade_no']; //商户订单号
            $total_fee = $responseData['total_fee']; //订单金额/分
            $trade_no = $responseData['transaction_id']; //微信订单号
            $time = time();
            $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->field('id,ushare_id,store_id,ctm_id')->find();
            $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
            ->field('custom_phone')
            ->find();
            
            $data['pay_type']=  Order::ORDTYPE_5;
            $data['pay_time']=  $time;
            $data['wx_trade_no']=  $trade_no;
            $data['status']=  2;
            $data['start_time']=$time;
            $data['end_time']=$time+$ushare['duration']*3600;
            $saveusharecustom=UshareCustom::where('ord_num','=',$out_trade_no)->update($data);
            //助力成功发送消息
            $store_id=$usharecustom['store_id'];
            $order_id=$usharecustom['id'];
            $content = "您成功发起了【[".$itemdata['item_name']."]】优享助力活动，快去分享给更多好友，更快助力成功~";
            $msgContent = Utils::getColoredString($content);
            $pushContent = Utils::getPushString($content);
            $alias = $customdata['custom_phone'] . $store_id;
            $userid = $usharecustom['ctm_id'];
            Utils::sendServicenews(Servicenews::TITLEID_16, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                'title' => '活动消息',
                'content' => $pushContent,
                'alias_type' => Servicenews::ALIAS_TYPE_2,
                'alias' => $alias,
            ]);
            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';

            echo $xml;
        }
    }
    
    /*
     * 微信退款回调
     */

    public function wxrefundnotify() 
    {
        $responseData = (array) simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);
        //微信付款回调
        $str = $responseData['req_info'];
        $key = MD5('zxcvbnmlkjhgfdsa12345678iuytrewq');
        //$data=  $this->refund_decrypt($str, $key);  
        $decrypt = base64_decode($str, true);
        $data = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
        $arrdata = (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($responseData['return_code'] == 'SUCCESS' && $arrdata['refund_status'] == 'SUCCESS') {
            $out_trade_no = $arrdata['out_trade_no']; //商户订单号
            $refund_id = $arrdata['refund_id'];
            $out_refund_no = $arrdata['transaction_id']; //退款号
            $time = time();
            $updatastatus['wx_refund_no'] = $out_refund_no;
            $updatastatus['status'] = UshareCustom::USHARE_STATUS_5;
            $updatastatus['update_time'] = time();
            $updateusharecustom = UshareCustom::where('ord_num','=',$out_trade_no)->update($updatastatus);
            $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)
                ->field('id,ord_num,pay_money,pay_type,status,end_time,ushare_id,ctm_id,store_id')
                ->find();
            $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->field('item_id')->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
                ->field('custom_phone')
                ->find();
            $store_id=$usharecustom['store_id'];
            $order_id=$usharecustom['id'];
            //退款成功发送推送
            $content = "很遗憾，您发起的【[".$itemdata['item_name']."]】优享助力活动助力失败，已成功退款。";
            $msgContent = Utils::getColoredString($content);
            $pushContent = Utils::getPushString($content);
            $alias = $customdata['custom_phone'] . $store_id;
            $userid = $usharecustom['ctm_id'];
            Utils::sendServicenews(Servicenews::TITLEID_15, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                'title' => '活动消息',
                'content' => $pushContent,
                'alias_type' => Servicenews::ALIAS_TYPE_2,
                'alias' => $alias,
            ]);
            
            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        }
    }
    
    /*
     * h5微信退款回调
     */

    public function h5wxrefundnotify() 
    {
        $responseData = (array) simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);
        //微信付款回调
        $str = $responseData['req_info'];
        $key = MD5('iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5');
        //$data=  $this->refund_decrypt($str, $key);  
        $decrypt = base64_decode($str, true);
        $data = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
        $arrdata = (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($responseData['return_code'] == 'SUCCESS' && $arrdata['refund_status'] == 'SUCCESS') {
            $out_trade_no = $arrdata['out_trade_no']; //商户订单号
            $refund_id = $arrdata['refund_id'];
            $out_refund_no = $arrdata['transaction_id']; //退款号
            $time = time();
            $updatastatus['wx_refund_no'] = $out_refund_no;
            $updatastatus['status'] = UshareCustom::USHARE_STATUS_5;
            $updatastatus['update_time'] = time();
            $updateusharecustom = UshareCustom::where('ord_num','=',$out_trade_no)->update($updatastatus);
            $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)
                ->field('id,ord_num,pay_money,pay_type,status,end_time,ushare_id,ctm_id,store_id')
                ->find();
            $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->field('item_id')->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
                ->field('custom_phone')
                ->find();
            $store_id=$usharecustom['store_id'];
            $order_id=$usharecustom['id'];
            //退款成功发送推送
            $content = "很遗憾，您发起的【[".$itemdata['item_name']."]】优享助力活动助力失败，已成功退款。";
            $msgContent = Utils::getColoredString($content);
            $pushContent = Utils::getPushString($content);
            $alias = $customdata['custom_phone'] . $store_id;
            $userid = $usharecustom['ctm_id'];
            Utils::sendServicenews(Servicenews::TITLEID_15, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                'title' => '活动消息',
                'content' => $pushContent,
                'alias_type' => Servicenews::ALIAS_TYPE_2,
                'alias' => $alias,
            ]);
            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        }
    }
    
    /*
     * 小程序微信退款回调
     */

    public function xcxwxrefundnotify() 
    {
        $responseData = (array) simplexml_load_string(file_get_contents('php://input'), 'SimpleXMLElement', LIBXML_NOCDATA);
        //微信付款回调
        $str = $responseData['req_info'];
        $key = MD5('iK5Qz6W2I4NEXSqoyfBtOTSItg3oJbF5');
        //$data=  $this->refund_decrypt($str, $key);  
        $decrypt = base64_decode($str, true);
        $data = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
        $arrdata = (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        if ($responseData['return_code'] == 'SUCCESS' && $arrdata['refund_status'] == 'SUCCESS') {
            $out_trade_no = $arrdata['out_trade_no']; //商户订单号
            $refund_id = $arrdata['refund_id'];
            $out_refund_no = $arrdata['transaction_id']; //退款号
            $time = time();
            $updatastatus['wx_refund_no'] = $out_refund_no;
            $updatastatus['status'] = UshareCustom::USHARE_STATUS_5;
            $updatastatus['update_time'] = time();
            $updateusharecustom = UshareCustom::where('ord_num','=',$out_trade_no)->update($updatastatus);
            $usharecustom=UshareCustom::where('ord_num','=',$out_trade_no)
                ->field('id,ord_num,pay_money,pay_type,status,end_time,ushare_id,ctm_id,store_id')
                ->find();
            $ushare=Ushare::where('id','=',$usharecustom['ushare_id'])->field('item_id')->find();
            $itemdata=Item::where('id','=',$ushare['item_id'])->field('item_name')->find();
            $customdata = Custom::where('id', '=', $usharecustom['ctm_id'])
                ->field('custom_phone')
                ->find();
            $store_id=$usharecustom['store_id'];
            $order_id=$usharecustom['id'];
            //退款成功发送推送
            $content = "很遗憾，您发起的【[".$itemdata['item_name']."]】优享助力活动助力失败，已成功退款。";
            $msgContent = Utils::getColoredString($content);
            $pushContent = Utils::getPushString($content);
            $alias = $customdata['custom_phone'] . $store_id;
            $userid = $usharecustom['ctm_id'];
            Utils::sendServicenews(Servicenews::TITLEID_15, $order_id, 0, $msgContent, $userid, Servicenews::TYPE_2, $store_id, [
                'title' => '活动消息',
                'content' => $pushContent,
                'alias_type' => Servicenews::ALIAS_TYPE_2,
                'alias' => $alias,
            ]);
            $return = [
                'return_code' => 'SUCCESS',
                'return_msg' => 'OK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        } else {
            $return = [
                'return_code' => 'FAIL',
                'return_msg' => 'NOTOK',
            ];
            $xml = '<xml>';
            foreach ($return as $key => $value) {
                $xml = $xml . '<' . $key . '><![CDATA[' . $value . ']]></' . $key . '>';
            }
            $xml = $xml . '</xml>';
            echo $xml;
        }
    }


}